<?php
namespace app\admin\validate;

use think\Validate;
use app\admin\model\Worker as WM;

class WorkerValidate extends Validate
{
    protected $rule = [
        'username' => 'require',
        'passwd' => 'checkPasswd:passwd',
        'mobile' => 'require|checkMobile:mobile',
        'job_number' => 'require|checkJobNumber:job_number',
        'join_time' => 'require',
        'birth_time' => 'require'
    ];

    protected $message = [
      'username.require' => '请输入职员名称',
      'passwd.checkPasswd' => '密码必须8位以上字母数字组合',
      'mobile.require' => '请输入职员手机号',
      'mobile.checkMobile' => '手机号码已经存在或者不正确',
      'job_number.require' => '请输入职员工号',
      'job_number.checkJobNumber' => '工号已经存在或者不正确',
      'join_time.require' => '请填写入职时间',
      'birth_time.require' => '请填写生日'
    ];

    protected $scene = [
        'savedata' => ['username','passwd', 'mobile', 'job_number', 'join_time', 'birth_time']
    ];

    /**
     * [checkPasswd 检测密码]
     * @param  [type] $value [description]
     * @param  [type] $rule  [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function checkPasswd($value, $rule, $data)
    {
        $passwdLength = function() use ($value) { return mb_strlen($value) < 8 ? false : true; };
        if ( isset($data['id']) && $data['id'] > 0 ) {
            if ( !empty($data['passwd']) ) {
                return $passwdLength();
            }
            return true;
        } else {
            return $passwdLength();
        }
    }

    /**
     * [checkMobile 验证手机号是否可以注册]
     * @param  [type] $value [description]
     * @param  [type] $rule  [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function checkMobile($value, $rule, $data)
    {
        if ( isset($data['id']) && $data['id'] > 0) {
            $worker = WM::where('id', '<>', $data['id'])->where('mobile', $value)->value('id');
            if ($worker ) {
                return false;
            }
        } else {
            if ( !preg_match('/^1[345789]\d{9}$/', $value) ) {
                return false;
            }
            $worker = WM::where('mobile', $value)->value('id');
            if ($worker > 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * [checkJobNumber 检测工作号是否正确]
     * @param  [type] $value [description]
     * @param  [type] $rule  [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function checkJobNumber($value, $rule, $data)
    {
        if ( isset($data['id']) && $data['id'] > 0 ) {
            $worker = WM::where('id', '<>', $data['id'])->where('job_number', $value)->value('id');
            if ( $worker > 0 ) {
                return false;
            }
        } else {
            if ($value <= 0) {
                return false;
            }
            $worker = WM::where('job_number', $value)->value('id');
            if ( $worker ) {
                return false;
            }
        }
        return true;
    }
}
